package org.game.db.proxy.bean;

import java.util.Date;
import java.util.List;

import com.jfinal.template.ext.directive.Now;
import org.cfg.DungeonLevelGroupCfg;
import org.cfg.cache.DungeonLevelGroupCfgCache;
import org.game.constant.DungeonConstant;
import org.game.db.bean.DungeonLevelGroup;

import com.google.common.collect.Lists;
import com.sojoys.artifact.build.data.base.BasePojoProxy;
import org.utils.TimeUtil;

/**
 * @author : DengYing
 * @CreateDate : 2017年11月30日 下午2:43:06
 * @Description ：Please describe this document
 */
public class DungeonLevelGroupProxy extends BasePojoProxy<DungeonLevelGroupProxy, DungeonLevelGroup>{

	public DungeonLevelGroupCfg getCfg(){
		return DungeonLevelGroupCfgCache.me().getById(t.getCid());
	}

	/**
	 * 战斗次数是否达到上限
	 */
	public boolean checkFightCountIsLimit(){
		if (getCfg().getCountLimit() == 0)
			return false;
		return t.getFightCount() >= getCfg().getCountLimit() + t.getBuyCount();
	}

	/**
	 * 查看奖励是否领取
	 */
	public boolean isGettedReward(int difficulty, int starNum){
		List<Integer> list = (List<Integer>) t.getGetedReward().get(difficulty);
		if (list != null){
			return list.contains(starNum);
		}
		return false;
	}

	public void recordReward(int difficulty, int starNum){
		List<Integer> list = (List<Integer>) t.getGetedReward().computeIfAbsent(difficulty, k-> Lists.newArrayList());
		list.add(starNum);
		t.update();
	}

	public void refreshCount(long nowTime){
		if (t.getDungeonLevelGroupProxy().getCfg().getCycleType() == DungeonConstant.FIGHT_COUNT_REFRESH_TYPE_DAILY) {
			if (TimeUtil.isSameDay(t.getLastRefreshTime().getTime(), nowTime)){
				return;
			}
			t.setFightCount(0);
			t.setBuyCount(0);
			t.setLastRefreshTime(new Date());
			t.update();
		}
	}
}
